#!/usr/bin/perl -w
package clreqd_modules;

use strict;
use lvm;
use cldb;

#########################################################################
# This is the code that lha_buildapp --verify eventually uses via 	#
# the other clreqd request 'lvm_attempt_use'.				#
#									#
# If simply calls the run_before_start_on_non_current on the 		#
# specified storage spec.						#
#########################################################################

sub ss_run_before_shutdown_on_non_current {
my %ARGS=@_;
my $REPL_MAP;
my $FS=undef;
my $LV=undef;
my $VG=undef;

	$FS=$ARGS{FS} if(exists($ARGS{FS}) && defined($ARGS{FS}));
	$VG=$ARGS{VG} if(exists($ARGS{VG}) && defined($ARGS{VG}));
	$FS=$ARGS{LV} if(exists($ARGS{LV}) && defined($ARGS{LV}));

	my $APP=$ARGS{APPLICATION};
	my $CLDB=new cldb("$::ENV{TRUECL_CFG}/config.db");
	if(!defined($CLDB)) {
		return "ERROR - Unable to connect to 'config' database.";
	}
	my $res=$CLDB->get_rows("SELECT * FROM applications WHERE name = '$APP';");
	my $res2;
	if(defined($FS)) {
		$res2=$CLDB->get_rows("SELECT * FROM application_fs WHERE name = '$APP' AND mountpnt = '$FS';");
		if(!defined($res2->[0]->{lv})) {
			return "ERROR - File system '$FS' is not defined for application '$APP'.";
		}
	}
	if(!defined($res->[0]->{nodes})) {
		return "ERROR - Application '$APP' is unknown.";
	}

	$CLDB->disconnect; $CLDB=undef;
	my $s=$res->[0]->{storage};
	#################################################################
	# If REPL_MAP has been passed get the infor from it.		#
	#################################################################
	if(exists($ARGS{REPL_MAP})) {
		$REPL_MAP=$ARGS{REPL_MAP};
	}
	#################################################################
	# Now open open the configuration database to get the storage	#
	# spec and list of volume groups...				#
	#################################################################

	if(! -f "$::ENV{TRUECL_CODE}/lib/storage_spec/$s.pm") {
		return "ERROR - Storage spec '$s' not available.";
	}

	eval "use storage_spec::$s;";
	my $STORAGE=eval "new storage_spec::$s;";
	if(!defined($STORAGE)) {
		return "ERROR - Unable to use storage spec - storage_spec::$s\nError: " . eval "storage_spec::${s}::get_error_msg;";
	}
	#################################################################
	# If a VG/LV pair has been specified use that instead.		#
	#################################################################
	if(defined($LV) && defined($VG)) {
		my ($rc,$err)=$STORAGE->run_before_shutdown_on_non_current($VG,$REPL_MAP,$LV);
		if(!$rc) {
			return "ERROR - $err from run_before_shutdown_on_non_current";
		}
		return "OK";
	}

	#################################################################
	# If an FS has been defined we just run against that one only.	#
	#################################################################
	if(defined($FS)) {
		my $VG=$res2->[0]->{vg};
		my $LV=$res2->[0]->{lv};
		my ($rc,$err)=$STORAGE->run_before_shutdown_on_non_current($VG,$REPL_MAP,$LV);
		if(!$rc) {
			return "ERROR - $err from run_before_shutdown_on_non_current";
		}
		return "OK";
	}
	#################################################################
	# We need to run against each vg separately.			#
	#################################################################
	my $VGS=$res->[0]->{vgs};
	my @VGLIST=split(/,/,$VGS);
	for $VG (@VGLIST) {
		my ($rc,$err)=$STORAGE->run_before_shutdown_on_non_current($VG,$REPL_MAP);
		if(!$rc) {
			return "ERROR - $err from run_before_shutdown_on_non_current";
		}
	}
	return "OK";
}

1;
